<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_VoucherController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "manage voucher";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
        
        $model = new Admin_Model_Voucher();
        $vouchers = $model->getDatas();
        
        $this->view->data = $vouchers;
	}
    
    public function createAction(){
        parent::addScripts(array('daterangepicker.js', 'moment.js', 'bootstrap-datepicker.js', 'jquery-price-format.min.js'));
        parent::addStyleSheet(array('datepicker.css', 'daterangepicker.css'));
        
        $request = $this->_request;
        
        if($request->isPost()){
            $validator = new Admin_Form_ValidateVoucher($request->getParams());
            
            if($validator->isError()){
                $this->view->isError = $validator->isError();
                $this->view->messageError = $validator->getMessageError();
                $this->view->classError = $validator->getClassError();
                $this->view->data = $validator->getData();
            }else{
                $model = new Admin_Model_Voucher();
                $rs = $model->saveData($validator->getData());
                
                $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'voucher', 'action' => 'index'), null, true);
            }
        }
    }
    
    public function editAction(){
        parent::addScripts(array('daterangepicker.js', 'moment.js', 'bootstrap-datepicker.js', 'jquery-price-format.min.js'));
        parent::addStyleSheet(array('datepicker.css', 'daterangepicker.css'));
        
        $request = $this->_request;
        
        $model = new Admin_Model_Voucher();
        $voucher = $model->getData($request->getParam('id'), array(), true);
        
        $this->view->data = $voucher;
        
        if($request->isPost()){
            $save = $request->getParam('save', false);
            
            if($save){
                $validator = new Admin_Form_ValidateVoucher($request->getParams(), true, $voucher);
                
                if($validator->isError()){
                    $this->view->isError = $validator->isError();
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->classError = $validator->getClassError();
                    $this->view->data = $validator->getData();
                }else{
                    $rs = $model->saveData($validator->getData());
                    
                    $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'voucher', 'action' => 'index'), null, true);
                }
            }
        }
    }
    
    public function updateStatusAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $params = $request->getParams();
            
            $data['id'] = $params['id'];
            
            if($params['status'] == 'active'){
                $data['status'] = true;
            }else{
                $data['status'] = false;
            }
            
            $model = new Admin_Model_Voucher();
            $rs = $model->saveData($data);
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'voucher', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deleteAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_Voucher();
            $rs = $model->deleteData($request->getParam('id'));
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'voucher', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
}